<?php

namespace app\admin\controller;

use app\common\entity\BackResult;
use app\common\service\VipService;
use app\Request;

/**
 * Class Vip
 * @package app\admin\controller
 */
class Vip extends Base
{
    /**
     * @param VipService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(VipService $service)
    {
        return BackResult::success($service->getList());
    }

    /**
     * @param Request $request
     * @param VipService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function one(Request $request, VipService $service)
    {
        return BackResult::success($service->getOne($request->param('id')));
    }

    /**
     * @param Request $request
     * @param VipService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, VipService $service)
    {
        return BackResult::success($service->save([
            'id' => (int)$request->param('id', 0),
            'state' => (int)$request->param('state', 1),
            'fee' => (float)$request->param('fee', 0),
            'origin_fee' => (float)$request->param('origin_fee', 0),
            'title' => trim($request->param('title', '')),
            'mark' => trim($request->param('mark', '')),
            'time_unit' => (int)$request->param('time_unit', 0),
            'time_expire' => (int)$request->param('time_expire', 0),
            'sort' => (int)$request->param('sort', 0),
        ]));
    }

    /**
     * @param Request $request
     * @param VipService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(Request $request, VipService $service)
    {
        $id = (int)$request->param('id');
        $field = trim($request->param('field'));
        $value = trim($request->param('value'));

        if (!$id || !($vip = $service->getOne($id))) {
            return BackResult::error(null, 'VIP数据不存在');
        }
        if ('id' === $field || !isset($vip[$field])) {
            return BackResult::error(null, '参数异常');
        }

        return BackResult::success($service->save([
            'id' => $id,
            $field => $value,
        ]));
    }

    /**
     * @param Request $request
     * @param VipService $service
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request, VipService $service)
    {
        return BackResult::success($service->delete((int)$request->param('id')));
    }
}